package com.prp.gwt.client.widgets.user.php;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.php.PHPListener;
import com.prp.gwt.client.vo.MessageList;
import com.prp.gwt.client.vo.User;

public class MsgBoardPanel extends DecoratorPanel {

	private static MsgBoardPanel instance;
	private VerticalPanel innerPanel = new VerticalPanel();
	private VerticalPanel editPanel = new VerticalPanel();
	private VerticalPanel messageListPanel = new VerticalPanel();
	private MessageList messageList;
	private Image loadImage = new Image(GWT.getModuleBaseURL() + "loading.gif");
	public static final int NUM_PER_PAGE = 8;
	private int index;
	private HTML head = new HTML("<a>首页</a>");
	private HTML next = new HTML("<a>下一页</a>");
	private HTML last = new HTML("<a>上一页</a>");
	private HTML tail = new HTML("<a>尾页</a>");
	private TextArea textArea = new TextArea();
	private User toUser;
	private User user;
	private boolean isReply = false;
	private Label word = new Label("0");
	
	public static MsgBoardPanel getInstance() {
		return instance;
	}

	public MsgBoardPanel() {
		instance = this;
		addStyleName("msgboard");
		head.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(1);
			}
		});
		tail.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(getPageCount());
			}
		});
		next.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(index + 1);
			}
		});
		last.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(index - 1);
			}
		});
		add(innerPanel);
		loadEditPanel();
		innerPanel.add(editPanel);
		innerPanel.add(loadImage);
		innerPanel.add(messageListPanel);
		innerPanel.addStyleName("page-frame");
		messageListPanel.addStyleName("page-frame");
	}

	private void loadEditPanel(){
		{
			HorizontalPanel hPanel = new HorizontalPanel();
			hPanel.addStyleName("msgboard-title");
			Label label = new Label("留言板");
			label.addStyleName("msgboard-title-text");
			hPanel.add(label);
			label = new Label("(");
			label.addStyleName("msgboard-title-text");
			hPanel.add(label);
			hPanel.add(word);
			word.addStyleName("msgboard-title-text");
			textArea.addKeyUpHandler(new KeyUpHandler(){
				@Override
				public void onKeyUp(KeyUpEvent event) {
					word.setText(""+textArea.getText().length());
				}
			});
			label = new Label("/300)");
			label.addStyleName("msgboard-title-text");
			hPanel.add(label);
			editPanel.add(hPanel);
		}
		textArea.setCharacterWidth(40);
		textArea.setVisibleLines(5);
		textArea.addStyleName("msgboard-textArea");
		editPanel.add(textArea);
		{
			HorizontalPanel hPanel = new HorizontalPanel();
			hPanel.setWidth("100%");
			HTML reset = new HTML("<a>清空</a>");
			reset.addStyleName("msgboard-text");
			hPanel.add(reset);
			reset.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					textArea.setText("");
					isReply = false;
				}
			});
			Button commit = new Button("留言");
			hPanel.add(commit);
			hPanel.setCellHorizontalAlignment(commit, HasHorizontalAlignment.ALIGN_RIGHT);
			commit.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					String str = textArea.getText();
					if(isReply){
						if(!str.startsWith("回复"+toUser.getUsername()+":"))
							str += "回复"+toUser.getUsername()+":";
					}
					int maxLength = 300;
					if (str.length() > maxLength) {
						Window.alert("超过字数将被舍弃！");
						str = str.substring(0, maxLength);
					}
					PHPListener.getInstance().addMessage(str, toUser.getId(), user.getId());
				}
			});
			editPanel.add(hPanel);
		}
	}
	
	public void setToUser(User user){
		toUser = user;
		textArea.setText("回复"+user.getUsername()+":");
		word.setText(""+textArea.getText().length());
		textArea.setFocus(true);
		isReply = true;
	}
	
	public User getCurrentUser(){
		return messageList.getCurrentUser();
	}
	
	public User getUser(){
		return user;
	}
	
	public void init() {
		textArea.setText("");
		textArea.setEnabled(false);
		word.setText("0");
		isReply = false;
		messageListPanel.clear();
		loadImage.setVisible(true);
	}
	
	public void refresh(){
		textArea.setText("");
		textArea.setEnabled(false);
		word.setText("0");
		isReply = false;
	}
	
	public void load(MessageList messageList) {
		loadImage.setVisible(false);
		textArea.setEnabled(true);
		this.messageList = messageList;
		this.user = messageList.getUser();
		toUser = user;
		toPage(1);
	}

	public void toPage(int index) {
		if (index > 0 && index <= getPageCount())
			this.index = index;
		else
			return;
		messageListPanel.clear();
		loadPagePanel();
		loadMessageItem();
	}

	public void loadMessageItem() {
		int start = (index - 1) * NUM_PER_PAGE;
		int end = Math.min(index * NUM_PER_PAGE, messageList.size());
		for (int i = start; i < end; i++)
			messageListPanel.add(new MsgItem(messageList.get(i)));
	}

	private void loadPagePanel() {
		messageListPanel.addStyleName("msgboard-panel");
		HorizontalPanel panel = new HorizontalPanel();
		panel.setSpacing(5);
		int start = 0;
		int end = 0;
		if (getPageCount() <= 5 || index < 3) {
			start = 1;
			end = Math.min(5, getPageCount());
		} else if (getPageCount() - index < 3) {
			start = getPageCount() - 4;
			end = getPageCount();
		} else {
			start = index - 2;
			end = index + 2;
		}
		panel.add(head);
		panel.add(last);
		if (index == 1) {
			head.removeStyleName("page");
			last.removeStyleName("page");
			head.addStyleName("page-disable");
			last.addStyleName("page-disable");
		} else {
			head.removeStyleName("page-disable");
			last.removeStyleName("page-disable");
			head.addStyleName("page");
			last.addStyleName("page");
		}
		for (int i = start; i <= end; i++) {
			if (i == index) {
				HTML l = new HTML("<a>" + String.valueOf(i) + "</a>");
				l.addStyleName("page-selected");
				panel.add(l);
				continue;
			}
			HTML label = new HTML("<a>" + String.valueOf(i) + "</a>");
			label.setStylePrimaryName("page");
			label.setStylePrimaryName("page");
			panel.add(label);
			final int j = i;
			label.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					toPage(j);
				}
			});
		}
		panel.add(next);
		panel.add(tail);
		if (index == getPageCount()) {
			next.removeStyleName("page");
			tail.removeStyleName("page");
			next.addStyleName("page-disable");
			tail.addStyleName("page-disable");
		} else {
			next.removeStyleName("page-disable");
			tail.removeStyleName("page-disable");
			next.addStyleName("page");
			tail.addStyleName("page");
		}
		messageListPanel.add(panel);
		messageListPanel.setCellHorizontalAlignment(panel, HasHorizontalAlignment.ALIGN_RIGHT);
	}

	public int getPageCount() {
		return (messageList.size() - 1) / NUM_PER_PAGE + 1;
	}

}
